/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.dao.DaoDocumento;
import com.myapp.dao.DaoFacade;
import com.myapp.form.IncluirForm;
import com.myapp.model.Documento;
import com.myapp.util.ManipuladoraArquivo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author elio
 */
public class ModificarDocumentoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       IncluirForm myform = (IncluirForm) form;
       DaoFacade dao = DaoDocumento.getInstance();
       int id = Integer.parseInt(request.getParameter("id")); 
       try{
        Documento d = (Documento) dao.carregarUm(id, Documento.class);
        d.setDescricao(myform.getDescricao());
        d.setTipo(myform.getTipo());
        d.setNumRevisao(myform.getNumRevisao());
        dao.alterar(d);
       
        //salvando arquivo
        FormFile myFile = myform.getTheFile();
        byte[] fileData = myFile.getFileData();
        String fileName  = myFile.getFileName();
            
        ManipuladoraArquivo.criarArquivo(d.getId().toString(),fileName, fileData);
        
        return mapping.findForward(SUCCESS);
        }catch (Exception e){
        return mapping.findForward(FAILURE);
        }
    }
}
